﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Baza.EF.DAL;

namespace Baza.EF.BLL
{
  public  class DAOwners
  {


      //UPDATE KORISNICKIH PODATAKA

      public static void UpdateProfilePage(Owners own)
      {
          
          Connection.dm.ad_Owners_UPDATE(own.OwnerID, own.FirstName, own.LastName, own.Email, own.CellNumber, own.BusinessNumber, 
              own.HomeNumber, own.FaxNumber, own.AdditionalContactInfo,own.WebsiteURL, own.PasswordSalt, own.PasswordHash);
               
      }

      //Provjera ispravnosti email adrese 
      public static bool checkEmail(string email)
      {
          return Convert.ToBoolean(Connection.dm.ad_Owner_Check_Email(email).FirstOrDefault());
      }
      //Potvrda ili Otkazivanje Rezervacije

      public static void ConfirmReservation(int PreReservationID)
      {
          Connection.dm.ad_Owner_ConfirmReservation(PreReservationID);
      }
      public static void CancelReservation(int ReservationID)
      {
          Connection.dm.ad_Owner_CancelReservation(ReservationID);
      }

      //Ucitavanje Rezervacije . .  .
      public static int getNumberOfNewReservation_By_OwnerID(int ownerID)
      {
          return Convert.ToInt32(Connection.dm.ad_Owner_Count_Number_Of_Reservation(ownerID).FirstOrDefault());
      }

      public static List<ad_Owner_ConfirmReservation_By_OwnerID_Result> getReservationByID( int ownerID)
      {
          return Connection.dm.ad_Owner_ConfirmReservation_By_OwnerID(ownerID).ToList();
      }
      //Broj apartmana vlasnika

      public static int getApartmansCount(int  ownerID) {
          return Convert.ToInt32(Connection.dm.ad_Owners_Apartman_Count(ownerID).FirstOrDefault());
      }

      public static Owners getOwnerByID(int id)
      {
          return Connection.dm.ad_Owner_Get_By_ID(id).FirstOrDefault();
      }


      #region Dodavanje Apartmana i ucitavanje podataka referentnih tabela
      public static void ApartmanInsert(int ownerID, Apartments apartment, List<Prices> pricesList,
          List<Pictures> picturesList, List<Descriptions> descriptionsList,List<int> amenitiesListID , 
          List<int> nearByActivitiesListID,Address addres , Cities cities)
      {

          int citiID = Convert.ToInt32(Connection.dm.ad_Cit_INSERT(cities.CountryID, cities.Name).FirstOrDefault());
          int addressID = Convert.ToInt32(Connection.dm.ad_Address_INSERT(citiID, addres.StreetNumber, addres.Address1, addres.Address2, addres.ApartmentNumber).FirstOrDefault());

          //Dodavanje apartmana . . .
            int apartmanID = Convert.ToInt32(Connection.dm.ad_Owners_Apartments_INSERT(
              apartment.Title,apartment.ApartmentTypeID,apartment.Bathrooms,apartment.Toilets,apartment.MaxGuests,
              apartment.MaxAdults,apartment.MaxChildren,apartment.MaxBabies,addressID,apartment.CheckIn,
              apartment.CheckOut,apartment.Floor,apartment.Elevator,apartment.AvgCleaningTime,apartment.MinStay,
              apartment.MaxStay,apartment.RentalDetails,apartment.Inventory,apartment.ArrivalSheet,apartment.Beds,apartment.Bedrooms,apartment.ProfilePictures).FirstOrDefault());
          //Dodavanje apartmana vlasniku . . .
            Connection.dm.ad_Owners_Apartment_OwnerIDApartmanID(ownerID, apartmanID);
          //Dodavanje cijena i perioda . . .
          int priceID=0;
          for (int i = 0; i < pricesList.Count; i++)
          {
              priceID =Convert.ToInt32(Connection.dm.ad_Owners_Apartments_Prices_INSERT(pricesList[i].PeriodStart, pricesList[i].PeriodEnd,pricesList[i].Price, pricesList[i].currency_id).FirstOrDefault());
              if(priceID!=0)
                Connection.dm.ad_Owners_ApartmentID_PriceID_INSERT(apartmanID, priceID);
          }
          //Dodavanje slika za apartman!
          int picturesID=0;
          for (int i = 0; i < picturesList.Count; i++)
          {
              picturesID =Convert.ToInt32(Connection.dm.ad_Owners_Pictures_INSERT(picturesList[i].Url, picturesList[i].UrlThumbnail).FirstOrDefault());
              if(picturesID!=0)
                Connection.dm.ad_Owners_ApartmentIDPictureID_INSERT(apartmanID, picturesID);
          }
          //Dodavanje opisa za apartman . . .
          int descriptionID = 0;
          for (int i = 0; i < descriptionsList.Count; i++)
          {
              descriptionID = Convert.ToInt32(Connection.dm.ad_Owners_Apartments_Description_INSERT(descriptionsList[i].ShortPropertyDescription, descriptionsList[i].FullPropertyDescription, descriptionsList[i].AreaDescription,descriptionsList[i].Language).FirstOrDefault());
              if(descriptionID!=0)
                Connection.dm.ad_Owners_ApartmentIdDescriptionId_INSERT(apartmanID, descriptionID);
          }
          //Dodavanje dodataka za apartman . . .
          for (int i = 0; i < amenitiesListID.Count; i++)
          {
              Connection.dm.ad_Owners_Apartment_Amenities_INSERT(apartmanID, amenitiesListID[i]);
          }
          //Dodavanje aktivnosti u blizini apartmana . . .
          for (int i = 0; i < nearByActivitiesListID.Count; i++)
          {
              Connection.dm.ad_Owners_Apartment_NearByActivities_INSERT(apartmanID, nearByActivitiesListID[i]);
          }

          
      }
     
      public static int PicturesInsert(string url , string urlTham)
      {
          return Convert.ToInt32(Connection.dm.ad_Owners_Pictures_INSERT(url,urlTham).FirstOrDefault());
      }
      
      public static int getPicturesCount(int apartmanID)
      {
          return Convert.ToInt32(Connection.dm.ad_Owners_Pictures_COUNT(apartmanID).FirstOrDefault());
      }

      public static void deletePicture(int PictureID)
      {
          Connection.dm.ad_Owners_Pictures_Delete(PictureID);
      }

      public static List<NearbyActivities> getNearbyActivities()
      {
          return Connection.dm.ad_Owners_NearbyActivities_SELECT().ToList();
      }
      public static List<Amenities> getAmenities()
      {
          return Connection.dm.ad_Owners_Amenities_SELECT().ToList();

      }
      public static List<ApartmentType> getApartmentType()
      {
          return Connection.dm.ad_Owners_ApartmentType_SELECT().ToList();
      }
      public static List<Currency> getCurrency()
      {
          return Connection.dm.ad_Owners_Apartments_Currency_SELECT().ToList();
      }
      #endregion

      #region Ucitavanje Apartmana i Update Apartmana

      public static void UpdateApartman(
          int ownerID, Apartments apartment, List<Prices> pricesList,
          List<Pictures> picturesList, List<Descriptions> descriptionsList, List<int> amenitiesListID,
          List<int> nearByActivitiesListID, Address addres, Cities cities ) //potrebno je poslati nove podatke koji ce se UPDATE-ovat, ne slati one koji su vec ucitani  . . .
      {

          //procedura ce vratiti ID, grada, ukoliko postoji u bazi grad sa istim nazivom i istom drzavom , nece se ponovno dodavati, nego ce se vratiti samo ID . . 
          int citiID = Convert.ToInt32(Connection.dm.ad_Cit_INSERT(cities.CountryID, cities.Name).FirstOrDefault());
          //Takodjer isto kao i kod kod dodavanja grada, addresa ce vratiti ID , podaci se nece duplicirati!
          int addressID =Convert.ToInt32(Connection.dm.ad_Address_INSERT(citiID, addres.StreetNumber, addres.Address1, addres.Address2, addres.ApartmentNumber).FirstOrDefault());
              //Dodavanje apartmana . . .
              int apartmanID = apartment.ApartmentID;
              Connection.dm.ad_Owners_Apartments_UPDATE
            (apartment.ApartmentID,
            apartment.Title, apartment.ApartmentTypeID, apartment.Bathrooms, apartment.Toilets, apartment.MaxGuests,
            apartment.MaxAdults, apartment.MaxChildren, apartment.MaxBabies, addressID, apartment.CheckIn,
            apartment.CheckOut, apartment.Floor, apartment.Elevator, apartment.AvgCleaningTime, apartment.MinStay,
            apartment.MaxStay, apartment.RentalDetails, apartment.Inventory, apartment.ArrivalSheet, apartment.Beds, apartment.Bedrooms, apartment.ProfilePictures);

              //Dodavanje cijena i perioda . . .
              int priceID = 0;
          for (int i = 0; i < pricesList.Count; i++)
          {
              priceID = Convert.ToInt32(Connection.dm.ad_Owners_Apartments_Prices_INSERT(pricesList[i].PeriodStart, pricesList[i].PeriodEnd, pricesList[i].Price, pricesList[i].currency_id).FirstOrDefault());
              Connection.dm.ad_Owners_ApartmentID_PriceID_INSERT(apartmanID, priceID);
          }
          //Dodavanje slika za apartman!
          int picturesID = 0;
          for (int i = 0; i < picturesList.Count; i++)
          {
              picturesID = Convert.ToInt32(Connection.dm.ad_Owners_Pictures_INSERT(picturesList[i].Url, picturesList[i].UrlThumbnail).FirstOrDefault());
              Connection.dm.ad_Owners_ApartmentIDPictureID_INSERT(apartmanID, picturesID);
          }
          //Dodavanje opisa za apartman . . .
          int descriptionID = 0;
          for (int i = 0; i < descriptionsList.Count; i++)
          {
              descriptionID = Convert.ToInt32(Connection.dm.ad_Owners_Apartments_Description_INSERT(descriptionsList[i].ShortPropertyDescription, descriptionsList[i].FullPropertyDescription, descriptionsList[i].AreaDescription, descriptionsList[i].Language).FirstOrDefault());
              Connection.dm.ad_Owners_ApartmentIdDescriptionId_INSERT(apartmanID, descriptionID);
          }
          //Dodavanje dodataka za apartman . . .
          //prvo cemo obrisati sve do sada koje su bili dodaci za apartman, pa tek onda nove dodati 
          Connection.dm.ad_Owner_Apartments_Amenities_DELETE(apartmanID);
          for (int i = 0; i < amenitiesListID.Count; i++)
          {
              Connection.dm.ad_Owners_Apartment_Amenities_INSERT(apartmanID, amenitiesListID[i]);
          }
          //Dodavanje aktivnosti u blizini apartmana . . .
          //prvo obrisati sve aktivnosti koje su u bile, i nove dodati. . 
          Connection.dm.ad_Owner_Apartments_NearbyActivities_DELETE(apartmanID);
          for (int i = 0; i < nearByActivitiesListID.Count; i++)
          {
              Connection.dm.ad_Owners_Apartment_NearByActivities_INSERT(apartmanID, nearByActivitiesListID[i]);
          }

      }


      public static List<NearbyActivities> getNearbyActivitiesByApartmanID(int apartmanID)
      {
          return Connection.dm.ad_Owners_Apartmens_CheckedNearbyActivities(apartmanID).ToList();
      }
      public static List<Amenities> getAmenitiesByApartmanID (int apartmanID)
      {
          return Connection.dm.ad_Owners_Apartmens_CheckedAmenities(apartmanID).ToList();
      }
      
      public static List<Descriptions> getDescriptionByApartmanID(int apartmanID)
      {
         return Connection.dm.ad_Owners_Apartmens_Get_Description_By_ApartmanID(apartmanID).ToList();
      }
           
      public static List<Pictures> getPicturesByApartmanID(int apartmanID)
      {
          return Connection.dm.ad_Owners_Apartmens_Get_Pictures_By_ApartmanID(apartmanID).ToList();
      }
      public static Cities getCityByCityID (int CityID)
      {
          return Connection.dm.ad_Owners_Apartmans_Get_Cities_By_CityID(CityID).FirstOrDefault();
      }
     
      public static Address getAddresbyAddresID ( int addresID )
      {
          return Connection.dm.ad_Owners_Apartmans_Get_Address_By_AddresID(addresID).FirstOrDefault();

      }
      public static void DeletePicturesByPicturesID (int picID , int apartmanID)
      {
          Connection.dm.ad_Owners_Apartmens_Pictures_DELETE_By_PictureID(picID, apartmanID);
      }
      public static void DeleteDescriptionByDescriptionID (int descID , int apartmanID)
      {
          Connection.dm.ad_Owners_Apartmens_Descriptions_DELETE_By_DescriptionID(descID,apartmanID);
      }
      public static void DeletePriceByPriceID (int priceID , int apartmanID)
      {
          Connection.dm.ad_Owners_Apartmens_Prices_DELETE_BY_PriceID(priceID,apartmanID);
      }

      public static List<ad_Owner_Apartmens_Prices_SELECT_BY_ApartmanID_Result> getAllPricesByApartmanID(int apartmanID )
      {
          return Connection.dm.ad_Owner_Apartmens_Prices_SELECT_BY_ApartmanID(apartmanID).ToList();
      }

      public static int getTotalCountApartmans(int ownerID)
      {
          return Convert.ToInt32(Connection.dm.ad_Owners_Get_Total_Count(ownerID).FirstOrDefault());
      }
      public static List<ad_Owners_SELECT_All_By_OwnerID_Result> getAllApartmansByOwnerID(int ownerID , int start , int max,string title)
      {
          return Connection.dm.ad_Owners_SELECT_All_By_OwnerID(ownerID, start, max, title).ToList();

      }

      public static ad_Owner_getNameByID_Result getOwnerNameByID(int ownerID )
      {
          return Connection.dm.ad_Owner_getNameByID(ownerID).FirstOrDefault();

      }

      public static Apartments getApartmanByID ( int apartmanID )
      {
          return Connection.dm.ad_Owners_Apartman_SELECT_By_ApartmanID(apartmanID).FirstOrDefault();

      }


      public static void DeleteApartman_By_ApartmanID (int apartmanID)
      {
          Connection.dm.ad_Owners_Apartmen_DELETE_By_ApartmanID(apartmanID);
      }

      #endregion


      

  }
}
